import Vue from "vue"
import Vuex from "vuex"
import { SET_STUDENT_MESSAGE, SET_TOKEN, LOGOUT, SET_LAST_TEST_MESSAGE, IS_GLASSES, SET_TRAINER } from "@/common/index/config/Event.js"
import { parseVisionCompanyToindex } from "@/common/index/utils/common.js"
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		name: '张三',
		sex: "男",
		_class: "六年级2班",
		school: "广州城建学院",
		address: "",
		nativeData: null,
		token: null,
		shopId: null,
		shop_id: null,
		lastTestMessage: null,
		lastTestVisions: null,
		testResult: ["4.0", "4.0", "4.0", "4.0", ""],
		trainers: null
	},
	mutations: {
		[SET_STUDENT_MESSAGE] (state, studentMessage) {
			state.nativeData = studentMessage
			const c = studentMessage.class_info
			state.name = studentMessage.name
			state.sex = studentMessage.sex == 1 ? "男" : "女"
			state._class = c ? `${c.grade}年级${c.classes}班` : ""
			state.school = studentMessage.school_or_unit
			state.shopId = studentMessage.shop_id
			state.shop_id = studentMessage.shop_id

		},
		[SET_TOKEN] (state, token) {
			state.token = token
		},
		[LOGOUT] (state) {
			state.name = ""
			state.sex = ""
			state._class = ""
			state.school = ""
			state.token = null
			state.nativeData = null
			state.lastTestMessage = null
			state.testResult = ["4.0", "4.0", "4.0", "4.0", ""]
		},
		[SET_LAST_TEST_MESSAGE] (state, message) {
			state.lastTestMessage = message
			state.lastTestVisions = []
			if (message.naked_vision && message.condenser_vision) {
				state.lastTestVisions.push(parseVisionCompanyToindex(message.naked_vision.left_eye.vision))
				state.lastTestVisions.push(parseVisionCompanyToindex(message.naked_vision.right_eye.vision))
				state.lastTestVisions.push(parseVisionCompanyToindex(message.condenser_vision.left_eye.vision))
				state.lastTestVisions.push(parseVisionCompanyToindex(message.condenser_vision.right_eye.vision))
			} else {
				state.lastTestVisions.push(0)
				state.lastTestVisions.push(0)
				state.lastTestVisions.push(0)
				state.lastTestVisions.push(0)
			}
		},
		[SET_TRAINER] (state, playload) {
			state.trainers = playload
		}
	},
	getters: {
		[IS_GLASSES] (state) {
			return state.nativeData.is_glasses == 1
		}
	}
})

uni.$store = store
export default store
